﻿// Allows DOM elements to be dropped in this one during a drag and drop operation
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    var Droppable = Wjs.defineOnce({

        type: 'Droppable',

        internal: {

            construct: function (cfg, parent) {

                cfg = cfg || {};

                if (cfg.droppable) {
                    this.setDroppable(cfg);
                }
            }
        },

        setDroppable: function (cfg) {
            var me = this;

            me.onDragEnter = cfg.onDragEnter || me.onDragEnter;
            me.onDragOver = cfg.onDragOver || me.onDragOver;
            me.onDragLeave = cfg.onDragLeave || me.onDragLeave;
            me.onDrop = cfg.onDrop || me.onDrop;

            me.addEvents({

                dragenter: function (evt) {

                    evt.preventDefault();

                    // Set dropEffect
                    //me.addClass(evt.target, cls);

                    if (me.onDragEnter) {
                        me.onDragEnter(evt);
                    }
                },

                dragover: function (evt) {

                    evt.preventDefault();
                    evt.dataTransfer.dropEffect = 'move';

                    if (me.onDragOver) {
                        me.onDragOver(evt);
                    }
                },

                dragleave: function (evt) {

                    //me.removeClass(evt.target, cls);

                    if (me.onDragLeave) {
                        me.onDragLeave(evt);
                    }
                },

                drop: function (evt) {
                    evt.preventDefault();

                    //me.removeClass(evt.target, cls);

                    console.log('drop event: ' + evt.target.outerHTML);

                    if (me.onDrop) {
                        me.onDrop(evt);
                    }
                }
            });
        }
    });

    return Droppable;
});